﻿using System;
using System.Collections.Generic;
using System.Web.Script.Serialization;
using System.Xml.Linq;
//
using PerceptiveMCAPI.Types.Internal;
//
namespace PerceptiveMCAPI.Types
{
	public class campaignAbuseReportsOutput : Api_BaseOutput
	{
		public campaignAbuseReportsResults result { get; set; }
		public campaignAbuseReportsParms inputParms { get; private set; }
		//
		public campaignAbuseReportsOutput( campaignAbuseReportsInput input )
			: base( input )
		{
			result = new campaignAbuseReportsResults();
			inputParms = input.parms;
		}
		// --------------------------------------------------- tostring
		public override string ToString()
		{
			return string.Format( "Abuse count: {0}", this.result.total );
		}

		#region ================================================================== format setup

		// ---- serial
		internal void Format( string api_Request, string api_Response )
		{
			this.api_Request = api_Request;
			this.api_Response = api_Response;
			// ----------------------------------
			if ( this.api_OutputType == EnumValues.OutputType.JSON )
				outputJSON();
			else if ( this.api_OutputType == EnumValues.OutputType.XML )
				outputXML();
		}

		// ---- xmlrpc
		internal void Format( x_campaignAbuseReports rpcResult )
		{
			this.api_Request = EnumValues.NotSupplied;
			this.api_Response = EnumValues.NotSupplied;
			// ------------------------
			this.result = FormatResults( rpcResult );
		}

		#endregion

		#region ================================================================= Output

		// ---------------------------------------------------------- outputXML
		private void outputXML()
		{
			XElement xml = XElement.Parse( this.api_Response );
			// error check
			Api_Error error = apiSerial.GetXmlOutputError( xml );
			if ( error != null )
			{
				this.api_ErrorMessages.Add( error );
				return;
			}
			// -------------------------------------------- get values
			try
			{
				DateTime dt; int it;
				//
				int.TryParse( xml.Element( "total" ).Value, out it );
				result.total = it;
				//
				IEnumerable<XElement> data = xml.Element( "data" ).Elements( "struct" );
				foreach ( XElement entry in data )
				{
					campaignAbuseReportsResults.DataItem item = new campaignAbuseReportsResults.DataItem();
					DateTime.TryParse( entry.Element( "date" ).Value, out dt );
					item.date = dt;
					item.email = entry.Element( "email" ).Value;
					item.type = entry.Element( "type" ).Value;
					this.result.data.Add( item );
				}
			}
			catch ( Exception ex )
			{
				this.api_ErrorMessages.Add( apiException.FormatError( ex ) );
			}
		}

		// ---------------------------------------------------------- outputJSON
		private void outputJSON()
		{
			JavaScriptSerializer jss = new JavaScriptSerializer();
			try
			{
				if ( this.api_Response.Contains( EnumValues.JSONErrorString ) )
				{
					Api_Error err = jss.Deserialize<Api_Error>( this.api_Response );
					this.api_ErrorMessages.Add( err );
				}
				else
				{
					x_campaignAbuseReports jsonResult = jss.Deserialize<x_campaignAbuseReports>( this.api_Response );
					this.result = FormatResults( jsonResult );
				}
			}
			catch ( Exception ex )
			{
				this.api_ErrorMessages.Add( apiException.FormatError( ex ) );
			}
		}

		// ---------------------------------------------------------- FormatResults
		protected campaignAbuseReportsResults FormatResults( x_campaignAbuseReports x_results )
		{
			DateTime dt;
			campaignAbuseReportsResults results = new campaignAbuseReportsResults();
			results.total = x_results.total;
			//
			foreach ( var entry in x_results.data )
			{
				campaignAbuseReportsResults.DataItem item = new campaignAbuseReportsResults.DataItem();
				DateTime.TryParse( entry.date, out dt );
				item.date = dt;
				item.email = entry.email;
				item.type = entry.type;
				results.data.Add( item );
			}
			return results;
		}

		#endregion
	}
}
